/*
 *  Copyright (c) Iris Dev. team. All rights reserved.
 *  See http://www.communico.pro/license for details.
 *
 */
#include "ASMemcached.hpp"
#include <stdio.h>

#include <ASGetOpt.hpp>
#include <errno.h>
#include <strings.h>
using namespace CAS;
using namespace CAS_MOD;

//
// Options
//
const Options oOptions[] = {
                             { 'h', "host", C_HAS_ARG }, // Memcached host
                             { 'p', "port", C_HAS_ARG }, // Memcached port
                             { 0 }
                           };

void Usage(CHAR_P szProgname)
{
	fprintf(stderr, "Usage: %s -h <host> [-p <port>]\n", szProgname);
}

int main(int argc, char ** argv)
{
	// Usage
	if (argc == 1) { Usage(argv[0]); return EX_USAGE; }

	STLW::string sHost;
	INT_32       iPort = 0;

	UINT_32 iArgNum = 1;
	CCHAR_P szArgValue;
	for(;;)
	{
		INT_32 iOption = GetOpt(oOptions, iArgNum, argc, argv, szArgValue);
		if (iOption == -1) { break; }
		switch (iOption)
		{
			case 'h':
				sHost = szArgValue;
				break;
			case 'p':
				iPort = atoi(szArgValue);
				break;
			case '?':
			default:
				fprintf(stderr, "Unknown option `%s` or option with missed argument\n", argv[iArgNum]);
				return EX_CONFIG;
				break;
		}
	}

	if (sHost.empty()) { sHost = "127.0.0.1"; fprintf(stderr, "Host not set, using 127.0.0.1 as default\n"); }
	if (iPort == 0)    { iPort = 11211;       fprintf(stderr, "Port not set, using 11211 as default\n"); }

	Memcached oMemcached(sHost, iPort);

	fprintf(stderr, "Connect: %d\n", oMemcached.Connect());

	fprintf(stderr, "Disconnect: %d\n", oMemcached.Disconnect());

	fprintf(stderr, "IsConnected: %d\n", oMemcached.IsConnected());

	fprintf(stderr, "Connect: %d\n", oMemcached.Connect());

	fprintf(stderr, "IsConnected: %d\n", oMemcached.IsConnected());

	fprintf(stderr, "Set: %d\n", oMemcached.Set("f_o_o", "0123456789012345678901234567890123456789012345678901!", 3600));

	fprintf(stderr, "Set: %d\n", oMemcached.Set("f_o_o", " dolor sit amet", 3600));

	fprintf(stderr, "Add: %d\n", oMemcached.Add("f_o_o", " dolor sit amet, consectetuer adipiscing elit,", 3600));

	fprintf(stderr, "Append: %d\n", oMemcached.Append("f_o_o", " sed diam nonummy nibh euismod tincidunt", 3600));

	fprintf(stderr, "Prepend: %d\n", oMemcached.Prepend("f_o_o", "Lorem ipsum", 3600));

	fprintf(stderr, "Append: %d\n", oMemcached.Append("f_o_o", " ut laoreet dolore magna aliquam erat volutpat.", 3600));

	fprintf(stderr, "Set: %d\n", oMemcached.Set("bar", "123", 3600));

	INT_64 iValue = 3600;
	fprintf(stderr, "Incr: %d\n", oMemcached.Incr("bar", iValue));

	STLW::string sValue;
	fprintf(stderr, "Get: %d\n", oMemcached.Get("bar", sValue));
	fprintf(stderr, "GET: `%s`\n", sValue.c_str());

	iValue = 3600;
        fprintf(stderr, "Decr: %d\n", oMemcached.Decr("bar", iValue));

	fprintf(stderr, "Get: %d\n", oMemcached.Get("bar", sValue));
	fprintf(stderr, "GET: `%s`\n", sValue.c_str());

	fprintf(stderr, "Get: %d\n", oMemcached.Get("f_o_o", sValue));
	fprintf(stderr, "GET: `%s`\n", sValue.c_str());


	fprintf(stderr, "Del: %d\n", oMemcached.Del("f_o_o"));

	fprintf(stderr, "Del: %d\n", oMemcached.Del("bar"));

	fprintf(stderr, "Del: %d\n", oMemcached.Del("bar"));

return 0;
}
// End.

